﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace XYAC.Framework.MonitorTireMaterialManual.UC
{
    /// <summary>
    /// TableHeaderUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class Line2UserControl : UserControl
    {
        public static readonly DependencyProperty LineColorProperty =
            DependencyProperty.Register(
                "LineColor", typeof(Color),
                typeof(Line2UserControl),
                new PropertyMetadata(Colors.LightGray, OnLineColorChanged)                                                            
                );
        public static readonly DependencyProperty LineBrushProperty =
            DependencyProperty.Register(
                "LineBrush", typeof(Brush),
                typeof(Line2UserControl),
                new PropertyMetadata(Brushes.Black, OnLineBrushChanged)                                                              
                );
        public static readonly DependencyProperty LineWidthProperty =
            DependencyProperty.Register(
                "LineWidth", typeof(int),
                typeof(Line2UserControl),
                new PropertyMetadata(
                    defaultValue: 2, propertyChangedCallback:OnLineWidthChanged)
                );

        private static void OnLineBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Line2UserControl;
            //control.caption.Text = control.CaptionText;
            control?.DrawLine();
            //if (control != null)
            {
                // 当属性变更时，这里可以执行一些逻辑
            }
            //caption.SetValue(TextBlock.TextProperty, value);
        }
        private static void OnLineColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Line2UserControl;
            control?.DrawLine();
            //control.caption.Text = control.CaptionText;
            //if (control != null)
            {
                // 当属性变更时，这里可以执行一些逻辑
            }
            //caption.SetValue(TextBlock.TextProperty, value);
        }
        private static void OnLineWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Line2UserControl;
            control?.DrawLine();
            //control.LineWidth = control.CaptionText;
            //if (control != null)
            {
                // 当属性变更时，这里可以执行一些逻辑
            }
            //caption.SetValue(TextBlock.TextProperty, value);
        }

        public Line2UserControl()
        {
            InitializeComponent();
            this.SizeChanged += TableHeaderUserControl_SizeChanged;
        }

        private void TableHeaderUserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DrawLine();
            //drawText(ActualWidth - 40, 10, "项目",Colors.DarkGreen , myCanvas);//Color.FromRgb(200, 0, 0)
            //drawText(10, ActualHeight - 30, "规格", Colors.DarkGreen, myCanvas);
        }

        public int LineWidth
        {
            get { return (int)GetValue(LineWidthProperty); }
            set
            {
                SetValue(LineWidthProperty, value);
            }
        }
        public Color LineColor
        {
            get { return (Color)GetValue(LineColorProperty); }
            set
            {
                SetValue(LineColorProperty, value);

                //tagvalue.SetValue(TextBlock.TextProperty, value);
            }
        }

        public Brush LineBrush
        {
            get { return (Brush)GetValue(LineBrushProperty); }
            set
            {
                SetValue(LineBrushProperty, value);

                //tagvalue.SetValue(TextBlock.TextProperty, value);
            }
        }
        
        private void drawText(double x, double y, string text, Color color, Canvas canvasObj)
        {
            TextBlock textBlock = new TextBlock();
            textBlock.Text = text;
            textBlock.Foreground = new SolidColorBrush(color);
            textBlock.FontSize = 18;
            Canvas.SetLeft(textBlock, x);
            Canvas.SetTop(textBlock, y);
            canvasObj.Children.Add(textBlock);
        }

        private void DrawLine()
        {
            Line line = new Line();
            //Size rect = DesiredSize;// VisualTreeHelper.GetDescendantBounds(this);
            // 设置线条的起点
            line.X1 = 0;
            line.Y1 = ActualHeight - 0;
            //终点
            line.X2 = ActualWidth-0; // 横向长度
            line.Y2 = 0; // 纵向长度

            line.Stroke = LineBrush;
            line.StrokeThickness = LineWidth;
            myCanvas.Children.Clear();
            myCanvas.Children.Add(line);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DrawLine();
            //drawText(ActualWidth - 40, 10, "项目", Colors.DarkGreen, myCanvas);//Color.FromRgb(200, 0, 0)
            //drawText(10, ActualHeight - 30, "规格", Colors.DarkGreen, myCanvas);
        }
        /*
        增加以下类代码， 跟踪尺寸，无果！
        static TableHeaderUserControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TableHeaderUserControl), new FrameworkPropertyMetadata(typeof(TableHeaderUserControl)));
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            // 在这里可以自定义控件的布局
            return base.ArrangeOverride(finalSize);
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            // 在这里可以自定义控件的尺寸测量
            return new Size(100, 100);
        }
        */
    }
}
